
var roomtype =null;
var num =null;
var id =null;

var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/room_type/get_room_type_list.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0){
                        console.log(res);
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/room_type/room_type_pag_info.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },
        popAddRoom_Type: function () {      //添加房间类型弹窗
            $("#pop-add-room_type").modal();
        },
        popAddRoom_TypeSubmit: function () {//添加房间类型提交
            var che = /^[1-9]$/;
            var type = $("#pop-add-room_type-name").val();
            var all = $("#pop-add-room_type-all").val();

            if(type!="" && all!=""){
                if(!che.test(all)){
                    MyAlert("请填写正确的宿舍人数");
                }
                else{
                    $.ajax({
                        url: 'api/room_type/add_room_type.php',
                        type: 'POST',
                        data: {
                            token: token,
                            time: unixtime,
                            room_type:type,
                            num:all,
                        },
                        dataType: 'JSON',
                        beforeSend: function () {
                            $("#loading-mask").show();
                        },
                        error: function () {
                            $("#loading-mask").hide();
                        },
                        success: function (res) {
                            $("#loading-mask").hide();
                            if (res.err == 0) {
                                MyAlert(res.msg, function () {
                                    console.log(123);
                                    window.location.reload();
                                });

                            } else {
                                MyAlert(res.msg);
                            }
                        }
                    })
                }
            }
            else{
                MyAlert("请填写必填信息");
            }
        },
        popUpdataRoom_Type: function (item) {  //修改房间类型弹窗
            $("#pop-updata-room_type").modal();
            $("#pop-updata-room_type-name").val(item.info.room_type);
            $("#pop-updata-room_type-all").val(item.info.live_count);
            roomtype=item.info.room_type;
            num=item.info.live_count;
            id=item.info.id;
        },
        popUpdataRoom_TypeSubmit:function(item){
            var name = $("#pop-updata-room_type-name").val();
            var all= $("#pop-updata-room_type-all").val();
            if(name==roomtype && all==num){
                MyAlert("信息未修改！");
            }
            else if(name==""||all==""){
                MyAlert("请完善修改信息!");
            }
            else{
                MyConfirm("确定修改?", function (){
                    $.ajax({
                        url: "api/room_type/updataRoom_TypeAuthority.php",
                        type: 'POST',
                        data: {
                            token: token,
                            time: unixtime,
                            room_type:name,
                            live_:all,
                            id:id,
                        },
                        dataType: 'JSON',
                        beforeSend: function () {
                            $("#loading-mask").show();
                        },
                        error: function () {
                            $("#loading-mask").hide();
                            alert("请求超时");
                        },
                        success: function (res) {
                            $("#loading-mask").hide();
                            if (res.err == 0) {
                                MyAlert(res.msg, function () {
                                    console.log(123);
                                    window.location.reload();
                                });
                            } else {
                                MyAlert(res.msg);
                            }
                        }
                    })
                })
            }
        },

        deleteRoom_TypeAuthority(index){//删除房间类型
            MyConfirm("确定删除?", function () {
                $.ajax({
                    url: "api/room_type/deleteRoom_TypeInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index: index
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                console.log(123);
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },

    }
});

theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页